package com.up4years.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

/**
 * 日期工具类
  *
 */
public class DateUtil {
 	public static final String DEFAULT_FORMAT ="yyyy-MM-dd HH:mm:ss";
	public static final String LOCAL_FORMAT ="yyyy年MM月dd日HH时mm分ss秒";
	public static final String LOCAL_DATE_FORMAT ="yyyy年MM月dd日HH时mm分ss秒";
	public static final String LOCAL_TIME_FORMAT ="HH时mm分ss秒";
	/**
	 * 将日期转换为字符串
	 * @return
	 */
	public static String dateFormat(){
		return dateFormat(new Date(),DEFAULT_FORMAT);
	}
	public static String dateFormat(String pattern){
		return dateFormat(new Date(), pattern);
	}
	public static String dateFormat(Date date,String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	/**
	 * 字符串转换为日期
	 * @param source
	 * @return
	 * @throws ParseException
	 */
	public static Date str2date(String source) throws ParseException{
 		return str2date(source,DEFAULT_FORMAT);
	}
	/**
	 * 字符串转换为日期
	 * @param source
	 * @return
	 * @throws ParseException
	 */
	public static Date str2date(String source,String pattern) throws ParseException{
 		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
 		return sdf.parse(source);
	}
	
	/**
     * 增加月
     * @param date
     * @param i
     * @return
     */
    public static Date addMonths(Date date, int i) {
        return org.apache.commons.lang.time.DateUtils.addMonths(date, i);
    }
    
    /**
     * 增加小时
     * @param date
     * @param amount
     * @return
     */
    public static Date addHours(Date date,int amount){
    	return org.apache.commons.lang.time.DateUtils.addHours(date, amount);
    }
    /**
     * 比较dt1和dt2的时间差
     * @param dt1
     * @param dt2 未指定时，默认为当前时间
     * @return 3分钟 -2秒等
     * @throws ParseException
     */
    public static String diff(Date dt1,Date dt2) throws ParseException{
    	String time="";
    	/**未指定对比时间时，和当前时间做比较**/
  		dt2 = dt2==null?  new Date() :dt2;
 		long between = dt2.getTime() - dt1.getTime();
		long day = between / (24 * 60 * 60 * 1000);
		long hour = (between / (60 * 60 * 1000) - day * 24);
		long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long second = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		long month = day / 30;
		long year = month/12;
		if(Math.abs(year)< 1){
			if ( Math.abs(month)< 1) {
				if ( Math.abs(day)< 1) {
					if ( Math.abs(hour) < 1) {
						if ( Math.abs(min) < 1) {
							time = second + "秒";
							return time;
						}
						time = min + "分钟";
						return time;
					} else {
						time = hour + "小时";
						return time;
					}
				} else {
					time = day + "天";
					return time;
				}
			}
			else{
				time =  month + "个月";
				return time;
			}
		}
		else {
			time = year + "年";
			return time;
		}
    }
    public static String getDiff(Date dt1,Date dt2) throws ParseException{
    	String time="";
    	/**未指定对比时间时，和当前时间做比较**/
    	dt2 = dt2==null?  new Date() :dt2;
     	long between = dt2.getTime() - dt1.getTime();
 		long hourUnit = 1000*60*60;
		long minutUnit = 1000*60;
		long secondUnit = 1000;
		long hour = between/hourUnit;
		long min = (between%hourUnit)/minutUnit;
		long sec = (between%minutUnit)/secondUnit;

		if(Math.abs(hour)>0){
			time+=hour+"时";
		}
		if(Math.abs(min)>0){
			time+=min+"分";
		}
		if(Math.abs(sec)>0){
			time+=sec+"秒";
		}
		if(StringUtils.isBlank(time))time="0秒";
		return time;
     }
    /**
	 * 获得当前时间与指定时间的时间差
	 * @param date
	 * @return 3分钟 2秒等
	 * @throws ParseException
	 */
	public static String getDiff(Date date) throws ParseException {
		return getDiff(date,null);
	}
	@Test
	public void test() throws ParseException{
		
		String dt1 = "2015-11-25 23:50:25";
		String dt2 = "2015-11-25 22:54:26";
		Date date1 = DateUtil.str2date(dt1);
		Date date2 = DateUtil.str2date(dt2);
		System.out.println(getDiff(date1,date2));
	}
}


